package com.yf.template.util;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
public class VelocityUtil implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties p = new Properties();
        p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        Velocity.init(p);
    }

    public VelocityContext createContext() {
        VelocityContext context = new VelocityContext();

        return context;
    }

    public Template createTemplate(String templateName) {
        Template template = Velocity.getTemplate(templateName);
        return template;
    }


}
